/*
 * Copyright (C) 2010 Graham Allan
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package edu.strath.cis.grallan.findbugs.adaptiveranking.population;

import java.util.Collection;

import edu.umd.cs.findbugs.BugCollection;
import edu.umd.cs.findbugs.BugInstance;

/**
 * @author Graham Allan (grallan@cis.strath.ac.uk)
 */
public class PopulationExtractor {

	private final BugCollection bugCollection;

	private final Collection<AlertPopulationMap> alertPopulationMaps;

	/**
	 * Package private constructor, use static factory method to create instance. Or use
	 * 
	 * @see #instanceFor(BugCollection)
	 */
	/* package */PopulationExtractor(BugCollection bugCollection, Collection<AlertPopulationMap> alertPopulationMaps) {
		this.bugCollection = bugCollection;
		this.alertPopulationMaps = alertPopulationMaps;

		extractPopulations();
	}

	private void extractPopulations() {
		if (null == bugCollection)
			return;

		for (BugInstance bug : bugCollection) {
			for (AlertPopulationMap alertPopulationMap : alertPopulationMaps) {
				alertPopulationMap.addAlert(bug);
			}
		}
	}

	public BugCollection bugCollection() {
		return bugCollection;
	}

	public Collection<AlertPopulationMap> alertPopulationMaps() {
		return alertPopulationMaps;
	}

	public static PopulationExtractor instanceFor(BugCollection bugCollection) {
		return new PopulationExtractor(bugCollection, new PopulationMapFactory().createPopulationMaps());
	}

	public static PopulationExtractor prototype() {
		return instanceFor(null);
	}

	public PopulationExtractor cloneWith(BugCollection bugCollection) {
		return new PopulationExtractor(bugCollection, new PopulationMapFactory().createPopulationMaps());
	}

}
